﻿package {
	import com.janisRadins.asDraw.Curve;
	import com.janisRadins.asDraw.Path;
	import flash.display.GraphicsPath;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import mx.utils.ObjectUtil;
	/**
	 * @author Jānis Radiņš
	 */
	public class T extends Sprite {
		private var curve1:Curve = null;
		private var curve2:Curve = null;
		private var path:Path = null;
		private var curveSprite:Sprite = null;
		
		private var point1Sprite:Sprite = null;
		private var point2Sprite:Sprite = null;
		
		
		public function T() {
			super();
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			curve1 = new Curve(50, 50, 100, 0);
			curve2 = new Curve(50, -50, 100, 0);
			
			var vectors:Vector.<Curve> = new Vector.<Curve>();
			vectors.push(curve1, curve2, curve1, curve2, curve1, curve2);
			
			path = new Path(vectors);
			path.x = 50;
			path.y = 50;
			
			curveSprite = new Sprite();
			addChild(curveSprite);
			
			//curve1.visualControlY = 100;
			//curve2.visualControlY = -100;
			
			point1Sprite = new Sprite();
			addChild(point1Sprite);
			point1Sprite.graphics.beginFill(0x123456);
			point1Sprite.graphics.drawCircle(0, 0, 5);
			point1Sprite.x = path.x + curve1.visualControlX;
			point1Sprite.y = path.y + curve1.visualControlY;
			point1Sprite.buttonMode = true;
			point1Sprite.addEventListener(MouseEvent.MOUSE_DOWN, onP1MousClick);
			
			point2Sprite = new Sprite();
			addChild(point2Sprite);
			point2Sprite.graphics.beginFill(0xFFFF);
			point2Sprite.graphics.drawCircle(0, 0, 5);
			point2Sprite.x = path.x + curve1.anchorX + curve2.visualControlX;
			point2Sprite.y = path.y + curve1.anchorY + curve2.visualControlY;
			point2Sprite.buttonMode = true;
			point2Sprite.addEventListener(MouseEvent.MOUSE_DOWN, onP2MousClick);
			
			redrawPath();
			
			//addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		
		
		private function redrawPath():void {
			var instructions:GraphicsPath = path.getCurveSegment();
			curveSprite.graphics.clear();
			curveSprite.graphics.lineStyle(2, 0xFF0000);
			curveSprite.graphics.drawPath(instructions.commands, instructions.data, instructions.winding);
		}
		
		private function onP1MousClick(e:MouseEvent):void {
			point1Sprite.startDrag();
			stage.addEventListener(MouseEvent.MOUSE_MOVE, updateCurve1);
			stage.addEventListener(MouseEvent.MOUSE_UP, cancelP1Move);
		}
		
		private function updateCurve1(e:MouseEvent):void {
			//trace((curveSprite.mouseX - path.x) + " : " + (curveSprite.mouseY - path.y));
			curve1.visualControlX = curveSprite.mouseX - path.x;
			curve1.visualControlY = curveSprite.mouseY - path.y;
			redrawPath();
		}
		private function cancelP1Move(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, updateCurve1);
			stage.removeEventListener(MouseEvent.MOUSE_UP, cancelP1Move);
			point1Sprite.stopDrag();
		}
		
		private function onP2MousClick(e:MouseEvent):void {
			point2Sprite.startDrag();
			stage.addEventListener(MouseEvent.MOUSE_MOVE, updateCurve2);
			stage.addEventListener(MouseEvent.MOUSE_UP, cancelP2Move);
		}
		private function updateCurve2(e:MouseEvent):void {
			curve2.visualControlX = curveSprite.mouseX - path.x - curve1.anchorX;
			curve2.visualControlY = curveSprite.mouseY - path.y - curve1.anchorY;
			redrawPath();
		}
		private function cancelP2Move(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, updateCurve2);
			stage.removeEventListener(MouseEvent.MOUSE_UP, cancelP2Move);
			point2Sprite.stopDrag();
		}
		
		
		private function onEnterFrame(e:Event):void {
			curve2.controlY++;
			redrawPath();
		}
	}
}